package com.taotao.service.ipml;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.JsonUtils;
import com.taotao.mapper.TbItemParamItemMapper;
import com.taotao.pojo.TbItemParamItem;
import com.taotao.pojo.TbItemParamItemExample;
import com.taotao.pojo.TbItemParamItemExample.Criteria;
import com.taotao.service.IItemParamItemService;

@Service
public class ItemParamItemServiceIpml implements IItemParamItemService {
	private static final int String = 0;
	@Autowired
	private TbItemParamItemMapper itemParamItemMapper;

	@Override
	public TaotaoResult saveItemParamItem(TbItemParamItem itemParamItem) {
		// TODO Auto-generated method stub
		try {
			this.itemParamItemMapper.insert(itemParamItem);
			return TaotaoResult.ok();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException("添加规格信息发生异常");
		}
	}

	@Override
	public String createItemParamItemHtml(Long itemId) {
		// TODO Auto-generated method stub
		TbItemParamItem itemParamItem = null;
		List<Map> mapList = null;
		TbItemParamItemExample example = new TbItemParamItemExample();
		Criteria criteria = example.createCriteria();
		criteria.andItemIdEqualTo(itemId);
		List<TbItemParamItem> itemParamItems = this.itemParamItemMapper
				.selectByExampleWithBLOBs(example);
		if (itemParamItems != null && itemParamItems.size() > 0) {
			itemParamItem = itemParamItems.get(0);
		} else {
			return null;
		}
		if (itemParamItem != null) {
			String paramData = itemParamItem.getParamData();
			mapList = JsonUtils.jsonToList(paramData, Map.class);
		} else {
			return null;
		}

		StringBuilder sb = new StringBuilder();
		sb.append("<table class=\"Ptable\" width=\"100%\" cellspacing=\"1\" cellpadding=\"0\" border=\"0\">"
				+ "\n");
		sb.append("\t" + "<tbody>" + "\n");
		for (Map tbItemParamItem : mapList) {
			sb.append("\t" + "\t" + "<tr>" + "\n");
			sb.append("\t" + "\t" + "\t"
					+ "<th class=\"tdTitle\" colspan=\"2\">"
					+ tbItemParamItem.get("group") + "</th>" + "\n");
			sb.append("\t" + "\t" + "</tr>" + "\n");
			List<Map<String, String>> paramsMapList = (List<Map<java.lang.String, java.lang.String>>) tbItemParamItem
					.get("params");
			for (Map<String, String> paramsMap : paramsMapList) {

				sb.append("\t" + "\t" + "<tr>" + "\n");
				sb.append("\t" + "\t" + "\t" + "<td class=\"tdTitle\">"
						+ paramsMap.get("k") + "</td>" + "\n");
				sb.append("\t" + "\t" + "\t" + "<td>" + paramsMap.get("v")
						+ "</td>" + "\n");
				sb.append("\t" + "\t" + "</tr>" + "\n");
			}
		}
		sb.append("\t" + "</tbody>" + "\n");
		sb.append("</table>" + "\n");

		return sb.toString();
	}
}
